#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;
ll n, m, k;
vector<string> s;
ll ans = 0;
ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};
const ll N = 1100;
class jd
{
public:
    ll x, y, cnt;
};
char a[N][N];
ll vis[N][N][11], dis[N][N][11];
bool bfs()
{
    queue<jd> q;
    q.push({1, 1, 1});
    if (n == 1 && m == 1)
    {
        return false;
    }
    vis[1][1][1] = 1;
    while (!q.empty())
    {
        auto now = q.front();
        q.pop();

        for (ll i = 0; i < 4; i++)
        {
            ll nx = now.x + dx[i], ny = now.y + dy[i], ncnt = now.cnt+1;
            if (nx >= 1 && nx <= n && ny >= 1 && ny <= m)
            {
                if(ncnt>k)
                {
                    if(a[nx][ny]==a[now.x][now.y])
                    {
                        continue;
                    }
                    else
                        ncnt = 1;
                }
                else {
                    if(a[nx][ny]!=a[now.x][now.y])
                        continue;
                }

                if(vis[nx][ny][ncnt]==1)
                    continue;
                vis[nx][ny][ncnt] ++;
                dis[nx][ny][ncnt] = dis[now.x][now.y][now.cnt] + 1;
                if(nx==n&&ny==m)
                {
                    cout << dis[nx][ny][ncnt];
                    return true;
                }
                q.push({nx, ny, ncnt});
            }
        }
    }
    return false;
}
void solve()
{

    cin >> n >> m >> k;

    for (ll i = 1; i <= n; i++)
    {
        for (ll j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }

    if (bfs())
    {
       ;
    }
    else
    {
        cout << -1 << endl;
    }
}
int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
